package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.InfoPublishBean;
import com.jason.platform.supervision.beans.NotifyResp;
import com.jason.platform.supervision.task.QueryNotifyDetailTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.widght.TitleBar;

/**
 * @ClassName: NotifyDetailActivity
 * @Description: 通知详情
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-8-8 11:39:45
 */
public class NotifyDetailActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = NotifyDetailActivity.class.getSimpleName();

    public static final String KEY_NOTIFY_INFO = "key_roster_info";

    private MyHandler<NotifyDetailActivity> mHandler;

    private TitleBar mTitleBar;

    private TextView mTitleTextView,mDateTextView,mContentTextView,mAccessoryTextView,mImportTextView;

    private InfoPublishBean mInfoPublishBean;

    //内容

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_notify_detail);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
        mHandler = new MyHandler<NotifyDetailActivity>(this);
        mTitleTextView = (TextView) findViewById(R.id.notify_detail_title);
        mDateTextView = (TextView) findViewById(R.id.notify_detail_date);
        mContentTextView = (TextView) findViewById(R.id.notify_detail_content);
        mAccessoryTextView = (TextView) findViewById(R.id.notify_detail_accessory);
        mImportTextView = (TextView) findViewById(R.id.notify_detail_import);

        mContentTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInfoPublishBean != null && !TextUtils.isEmpty(mInfoPublishBean.getContentUrl())){
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(mInfoPublishBean.getContentUrl());
                    intent.setData(content_url);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mInfoPublishBean = (InfoPublishBean) getIntent().getSerializableExtra(KEY_NOTIFY_INFO);
        updateDate();
        QueryNotifyDetailTask task = new QueryNotifyDetailTask(this);
        task.type = Constant.InfoPublish.QUERY_DETAIL;
        task.mParams.put("id", "1");
        task.execute();
    }

    public void updateDate(){
        if (mInfoPublishBean != null){
            String name = mInfoPublishBean.getSTitle();
            if (!TextUtils.isEmpty(name)){
                mTitleBar.setDisplayName(name);
            }
            mTitleTextView.setText(name);
            mDateTextView.setText(mInfoPublishBean.getPubTime());
            mContentTextView.setText(mInfoPublishBean.getContentUrl());
            mAccessoryTextView.setText("暂无");
            if (!TextUtils.isEmpty(mInfoPublishBean.getIsMajor()) && "1".equals(mInfoPublishBean.getIsMajor())){
                mImportTextView.setText("重要");
                mImportTextView.setTextColor(getResources().getColor(R.color.text_content_import_color));
            }else {
                mImportTextView.setText("不重要");
                mImportTextView.setTextColor(getResources().getColor(R.color.text_content_normal_color));
            }
        }
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.InfoPublish.QUERY_DETAIL){
            if (task.isSuccess) {
                NotifyResp resp = (NotifyResp) task.resData;
                mInfoPublishBean = resp.getValues().get(0);
                updateDate();
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_header_search_btn){
            //TODO 调用接口，进行搜索
        }
    }
}
